// pages/shop/shop_detail/shop_detail.js
var api = require('../../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: '',
    info:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      goodsId: options.id
    })
    this.getGoods()
  },
  getGoods() {
    api.get('/goods/detail', {
      id: this.data.goodsId
    }).then(res => {
      this.setData({
        info: res.detail
      })
    })
  },
  userCollect(){
    api.post('/user/collect/'+this.data.goodsId).then(res=>{
      this.setData({
        'info.collect_exists':res.is_collect
      })
    })
  },
  buyNow(){
    let info = this.data.info;
    info.num = 1;
    info.selected = true;
    let cars = [info];
  
    wx.setStorageSync('buyNowCars', cars);
    wx.navigateTo({
      url: '/pages/shop/create_order/create_order?type=buyNow',
    })
    
  },
  addCar(){
    let cars = wx.getStorageSync('cars') || [];
    this.data.info.num = 1;
    this.data.info.selected = 1;
    if(cars.length == 0){
      cars.push(this.data.info);
    }else{
      cars.map(item=>{
        if(item.id == this.data.info.id){
          item.num = item.num +1;
        }else{
          cars.push(this.data.info)
        }
      })
    }
    wx.setStorageSync('cars', cars);
    wx.showToast({
      title: '成功加入购物车',
      icon:'none'
    })
  },
  gotoCar(){
    wx.switchTab({
      url: '/pages/shop/shop_car/shop_car',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})